package com.example.wjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wjh.entity.Result;
import com.example.wjh.entity.TrainingSchedule;
import com.example.wjh.mapper.TrainingScheduleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/trainingschedule")
public class TrainingScheduleController {

    @Autowired
    private TrainingScheduleMapper trainingScheduleMapper;

    // 计算未完成训练安排数量
    @GetMapping("/incomplete/count")
    public Result getIncompleteTrainingCount() {
        QueryWrapper<TrainingSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "未完成");
        long count = trainingScheduleMapper.selectCount(queryWrapper);
        return Result.success(count);
    }

    // 审批训练安排
    @PutMapping("/approve/{id}")
    public String approveTrainingSchedule(@PathVariable Long id) {
        // 使用 UpdateWrapper 更新状态为通过
        int rows = trainingScheduleMapper.update(null, new UpdateWrapper<TrainingSchedule>()
                .eq("id", id)
                .set("status", "通过"));
        return rows > 0 ? "审批成功" : "审批失败";
    }

    // 获取指定 ID 的训练安排信息
    @GetMapping("/{id}")
    public TrainingSchedule getTrainingScheduleById(@PathVariable Long id) {
        return trainingScheduleMapper.selectById(id);
    }

    // 添加新训练安排
    @PostMapping("/add")
    public String addTrainingSchedule(@RequestBody TrainingSchedule trainingSchedule) {
        int rows = trainingScheduleMapper.insertTrainingSchedule(trainingSchedule);
        return rows > 0 ? "训练安排添加成功" : "训练安排添加失败";
    }

    // 更新训练安排信息
    @PutMapping("/update/{id}")
    public String updateTrainingSchedule(@PathVariable Long id, @RequestBody TrainingSchedule trainingSchedule) {
        trainingSchedule.setId(id);  // 使用 setId 方法
        int rows = trainingScheduleMapper.updateTrainingSchedule(trainingSchedule);
        return rows > 0 ? "训练安排更新成功" : "训练安排更新失败";
    }

    // 删除指定 ID 的训练安排
    @DeleteMapping("/delete/{id}")
    public String deleteTrainingSchedule(@PathVariable Long id) {
        int rows = trainingScheduleMapper.deleteById(id);
        return rows > 0 ? "训练安排删除成功" : "训练安排删除失败";
    }

    // 批量删除
    @DeleteMapping("/delete/batch")
    public int deleteBatch(@RequestBody List<Long> ids) {
        return trainingScheduleMapper.deleteBatchIds(ids);
    }

    // 获取所有训练安排信息
    @GetMapping("/all")
    public List<TrainingSchedule> getAllTrainingSchedules() {
        return trainingScheduleMapper.getAllTrainingSchedules();
    }

    // 分页获取训练安排信息
    @GetMapping("/page")
    public IPage<TrainingSchedule> getTrainingScheduleList(@RequestParam("pageNum") Integer pageNum,
                                                           @RequestParam("pageSize") Integer pageSize) {
        Page<TrainingSchedule> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TrainingSchedule> queryWrapper = new QueryWrapper<>();
        return trainingScheduleMapper.selectPage(page, queryWrapper);
    }

    // 多条件搜索训练安排
    @GetMapping("/search")
    public List<TrainingSchedule> searchTrainingSchedules(
            @RequestParam(required = false) String playerName,
            @RequestParam(required = false) String coachName,
            @RequestParam(required = false) String trainingLocation,
            @RequestParam(required = false) String status) {
        return trainingScheduleMapper.searchTrainingSchedules(playerName, coachName, trainingLocation, status);
    }
}

